package worktime.yft.demo.Service;

import net.minidev.json.JSONArray;
import net.minidev.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import worktime.yft.demo.Bean.UserAndDepartmentBean;
import worktime.yft.demo.Entity.User;
import worktime.yft.demo.Mapper.UserMapper;

@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    TokenService tokenService;

    public User findUser(String id){
        User u;
        try {
            u = userMapper.findUser(id);
        }
        catch (Exception e){
            System.out.println(e.getMessage());
            return null;
        }
        return u;
    }
    public JSONObject Login(String name,String psw){
        JSONObject object = new JSONObject();
        User u = userMapper.findUser(name);
        if(u == null)
        {
            object.put("message","登陆失败，用户名不存在！");
            object.put("status",0);
        }
        else if(!u.getPassword().equals(psw))
        {
            object.put("message","登陆失败，密码错误！");
            object.put("status",0);
        }
        else{
            String Token = tokenService.getToken(u);
            object.put("Token",Token);
            object.put("id",u.getUserId());
            object.put("name",u.getUserName());
            object.put("department",u.getDepartment());
            object.put("rights",u.isRights());
            object.put("status",1);
        }
        //进行登陆验证
        return object;
    }

    public boolean updatePsw(String id,String old,String password){
        try {
            User u = userMapper.findUser(id);
            if(u.getPassword().equals(old)){
                userMapper.updatePsw(id,password);
                return true;
            }
            else{
                return false;
            }
        }
        catch (Exception e){
            System.out.println(e.getMessage());
            return false;
        }
    }

    public JSONArray findUserAndDepartment(){
        JSONObject object;
        JSONArray array = new JSONArray();
        try{
            for(UserAndDepartmentBean userAndDepartmentBean :userMapper.findUserAndDepartment()){
                object = new JSONObject();
                object.put("department", userAndDepartmentBean.getDepartment());
                String[] users = userAndDepartmentBean.getUsers().split(",");
                String[] names = new String[users.length];
                String[] ids = new String[users.length];
                for(int i=0;i<users.length;i++){
                    String[] temp = users[i].split("-");
                    names[i] = temp[0];
                    ids[i] = temp[1];
                }
                object.put("username",names);
                object.put("userid",ids);
                array.add(object);
            }
            return array;
        }catch (Exception e){
            System.out.println(e.getMessage());
            return null;
        }
    }

    public JSONObject findDepartments(){
        JSONObject object = new JSONObject();
        try{
            object.put("departments",userMapper.findDepartment());
            return object;
        }catch (Exception e){
            System.out.println(e.getMessage());
            return null;
        }
    }

    public void insertUser(User user){
        userMapper.insert(user);
    }
}
